package walletuserslogic

import (
	"context"
	"github.com/shopspring/decimal"
	"go.uber.org/zap"
	"navi-mumbai/app/rpc/model/pg/wallet"
	"navi-mumbai/common/key/dbkey"
	"navi-mumbai/common/key/walletkey"
	"navi-mumbai/common/utils"
	"navi-mumbai/common/xerr"

	"navi-mumbai/app/rpc/wallet/internal/svc"
	"navi-mumbai/app/rpc/wallet/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetUserGameWalletListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetUserGameWalletListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserGameWalletListLogic {
	return &GetUserGameWalletListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 用户获取G端游戏钱包列表
func (l *GetUserGameWalletListLogic) GetUserGameWalletList(in *pb.GetUserGameWalletListReq) (*pb.GetUserGameWalletListResp, error) {
	defer utils.RecoverPanic()

	var err error
	var list []*wallet.UserGameWallet
	list, err = l.svcCtx.UserGameWalletModel.Find(l.ctx, in.PageNo, in.PageSize, in.Uid, 0, dbkey.Enable)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}

	var rsp pb.GetUserGameWalletListResp
	rsp.List = make([]*pb.GameWallet, 0)
	if len(list) <= 0 {
		return &rsp, nil
	}

	for i := 0; i < len(list); i++ {
		var gw pb.GameWallet
		gw.MoneyType = list[i].MoneyType
		dMoneyMount := decimal.NewFromInt(list[i].MoneyAmount)
		gw.Balance = dMoneyMount.Div(decimal.NewFromInt(walletkey.MoneyScale)).StringFixedBank(2)
		gw.ProviderId = list[i].ProviderId
		gw.UpdateTime = list[i].UpdateTime.Format(dbkey.DateTimeCNFormat)

		rsp.List = append(rsp.List, &gw)
	}

	return &rsp, nil
}
